package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.StrategySign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IStrategySignService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/strategySigns")
public class StrategySignController {

    @Autowired
    private IStrategySignService strategySignService;

    /**
     * 新增标记
     * @param strategySign
     * @return
     */
    @PostMapping
    public JsonResult save(StrategySign strategySign) {
        JsonResult result = new JsonResult();
        //设置点赞或者收藏的时间
        strategySign.setUser(UserContext.getCurrentUser());
        try {
            strategySignService.save(strategySign);
            HashMap<Object, Object> map = new HashMap<>();
            int greatStatus = strategySignService.queryGreatStatusCountByStrategyId(strategySign.getStrategy().getId());
            int enshrineStatus = strategySignService.queryEnshrineStatusCountByStrategyId(strategySign.getStrategy().getId());
            map.put("greatStatus", greatStatus);
            map.put("enshrineStatus", enshrineStatus);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 更新标记
     * @param strategySign
     * @return
     */
    @PutMapping("{id}")
    public JsonResult update(StrategySign strategySign) {
        JsonResult result = new JsonResult();
        strategySign.setUser(UserContext.getCurrentUser());
        try {
            strategySignService.update(strategySign);
            HashMap<Object, Object> map = new HashMap<>();
            int greatStatus = strategySignService.queryGreatStatusCountByStrategyId(strategySign.getStrategy().getId());
            int enshrineStatus = strategySignService.queryEnshrineStatusCountByStrategyId(strategySign.getStrategy().getId());
            map.put("greatStatus", greatStatus);
            map.put("enshrineStatus", enshrineStatus);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询当前用户在大攻略下的标记状态
     *
     * @param strategyId
     * @return
     */
    @GetMapping("{strategyId}")
    public StrategySign selectByStrategyId(@PathVariable Long strategyId) {
        return strategySignService.selectByStrategyId(strategyId, UserContext.getCurrentUser().getId());
    }

    /**
     * 查询当前某个攻略的点赞和收藏总数
     *
     * @param strategyId
     * @return
     */
    @GetMapping("count/{strategyId}")
    public Map queryGreatStatusCountByStrategyId(@PathVariable Long strategyId) {
        HashMap<Object, Object> map = new HashMap<>();
        int greatStatus = strategySignService.queryGreatStatusCountByStrategyId(strategyId);
        int enshrineStatus = strategySignService.queryEnshrineStatusCountByStrategyId(strategyId);
        map.put("greatStatus", greatStatus);
        map.put("enshrineStatus", enshrineStatus);
        return map;
    }


}
